home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / comm / bbs / cit_src_7H21.lha / adduser.c < prev    next >
C/C++ Source or Header  |  1997-07-27  |  11KB  |  374 lines

  1. /*
  2.  *                              AddUser.C
  3.  *
  4.  * Adds a user to a C86 installation.
  5.  */
  6.  
  7. /*
  8.  *                              History
  9.  *
  10.  * 90Dec28  HAW V1 Created.
  11.  */
  12.  
  13. /*
  14.  *                              Contents
  15.  *
  16.  *      main()                  main manager
  17.  *      ShowArguments()         Usage stuff
  18.  *      ParseFile()             parses the input file
  19.  *      OnOff()                 parses an On/Off parameter
  20.  *      AddAccount()            adds a new account to the system
  21.  *      slideLTab()             inserts account into logTab
  22.  *      storeLog()              stores the current log record
  23.  *      crashout()              fatal error handler
  24.  */
  25.  
  26. #include "ctdl.h"    /* header file  */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <math.h>
  31. #include <ctype.h>
  32. #include <time.h>
  33. #include <proto/exec.h>
  34. #include <dos/dos.h>
  35. #include <pragmas/dos_pragmas.h>
  36. #include "exec/memory.h"
  37. #include "exec/ports.h"
  38. #include "exec/exec.h"
  39.  
  40.  
  41. extern CONFIG    cfg;                   /* A buncha variables */
  42. extern logBuffer logBuf;                /* Pippul buffer */
  43. extern logBuffer logTmp;                /* Pippul buffer */
  44. extern FILE      *logfl;                /* log file descriptor */
  45. extern LogTable  *logTab;
  46. extern int       thisLog;
  47. extern rTable    *roomTab;              /* RAM index of rooms */
  48.  
  49. void ShowArguments(void);
  50. void ParseFile(void);
  51. void AddAccount(void);
  52. char OnOff(char *where);
  53.  
  54. FILE *fd;
  55. char onConsole, remoteSysop;
  56.  
  57. /*
  58.  * main()
  59.  *
  60.  * This is the main manager for adduser.
  61.  */
  62. int main(int, char **);
  63. int main(argc, argv)
  64. int  argc;
  65. char **argv;
  66. {
  67.         SYS_FILE tempName;
  68.  
  69.         printf("Citadel Add User %s\n%s\n", VERSION_NAME, COPYRIGHT);
  70.  
  71.         if (argc != 2) {
  72.                 ShowArguments();
  73.                 exit(1);
  74.         }
  75.  
  76.         if ((fd = fopen(argv[1], "r")) == NULL) {
  77.                 printf("Couldn't open %s.\n", argv[1]);
  78.                 exit(1);
  79.         }
  80.  
  81.         if (!readSysTab(FALSE, TRUE)) exit(1);
  82.  
  83.         makeSysName(tempName, "ctdllog.sys",  &cfg.logArea);
  84.         openFile(tempName, &logfl );
  85.  
  86.         initLogBuf(&logBuf);
  87.         initLogBuf(&logTmp);
  88.  
  89.         ParseFile();
  90.  
  91.         AddAccount();
  92.  
  93.         writeSysTab();
  94.  
  95.         return 0;
  96. }
  97.  
  98. /*
  99.  * ShowArguments()
  100.  *
  101.  * This function shows the arguments for adduser.
  102.  */
  103. void ShowArguments()
  104. {
  105.         printf("Usage: adduser <filename>\n\n");
  106.   printf("The format of the file should be as follows\n\n");
  107.   printf("User name (string)\n");
  108.   printf("Password (string)\n");
  109.   printf("Screen width (numeric, in columns)\n");
  110.   printf("Expert setting (ON or OFF)\n");
  111.   printf("Floor setting (ON or OFF)\n");
  112.   printf("Linefeeds setting (ON or OFF)\n");
  113.   printf("Display Time on messages (ON or OFF)\n");
  114.   printf("Last Old on New toggle (ON or OFF)\n");
  115.   printf("\t<last line of required data>\n");
  116.   printf("\t<following data is optional>\n");
  117.   printf("Aide setting (ON or OFF) (default to OFF)\n");
  118.   printf("Number of NULLS (numeric) (default 0)\n");
  119.   printf("Number of LD credits (numeric) (default 0)\n");
  120.   printf("Net privileges (ON or OFF) (default OFF)\n");
  121.   printf("Door privileges (ON or OFF) (default OFF)\n");
  122.   printf("Download privileges (ON or OFF) (default OFF)\n");
  123.   printf("Permanent account (ON or OFF) (default OFF)\n");
  124.   printf("Half Duplex setting (ON or OFF) (default OFF)\n");
  125.   printf("Twit setting (ON or OFF) (default OFF)\n");
  126.   printf(".ECD setting (numeric) (default 0)\n");
  127.   printf(".ECZ setting (ON or OFF) (defaults to OFF)\n");
  128. }
  129.  
  130. char Required = TRUE;
  131. /*
  132.  * ParseFile()
  133.  *
  134.  * This function parses the input file.
  135.  */
  136. void ParseFile(void)
  137. {
  138.         char line[200], good;
  139.         int  i, h;
  140.  
  141.         zero_struct(logBuf.lbflags);    /* zero all flags       */
  142.         /* user name */
  143.         if (GetAString(line, sizeof line, fd) == NULL)
  144.                 crashout("File ended abruptly while reading username.");
  145.  
  146.         if (strLen(line) == 0)
  147.                 crashout("Name is not long enough.");
  148.  
  149.         if (strLen(line) > 19)
  150.                 crashout("Name is too long.");
  151.  
  152.         if (PersonExists(line) != ERROR)
  153.                 crashout("Person already exists.");
  154.  
  155.         strCpy(logBuf.lbname, line);
  156.  
  157.         /* User password */
  158.         if (GetAString(line, sizeof line, fd) == NULL)
  159.                 crashout("File ended abruptly while reading password.");
  160.  
  161.         if (strLen(line) < 3)
  162.                 crashout("Password is not long enough.");
  163.  
  164.         if (strLen(line) > 19)
  165.                 crashout("Password is too long.");
  166.  
  167.         h = hash(line);
  168.         for (i = 0, good = TRUE; i < cfg.MAXLOGTAB && good; i++) {
  169.                 if (h == logTab[i].ltpwhash) good = FALSE;
  170.         }
  171.  
  172.         if (!good)
  173.                 crashout("Password already exists.");
  174.  
  175.         strCpy(logBuf.lbpw, line);
  176.  
  177.         if (GetAString(line, sizeof line, fd) == NULL)
  178.                 crashout("File ended abruptly while reading screen width.");
  179.  
  180.         if (!isdigit(line[0]))
  181.                 crashout("The screen width doesn't seem to be a numeral.");
  182.  
  183.         logBuf.lbwidth = atoi(line);
  184.         logBuf.lbflags.EXPERT = OnOff("Expert setting");
  185.         logBuf.lbflags.FLOORS = OnOff("Floors setting");
  186.         logBuf.lbflags.LFMASK = OnOff("Linefeeds setting");
  187.         logBuf.lbflags.TIME = OnOff("Time setting");
  188.         logBuf.lbflags.OLDTOO = OnOff("Old on New display setting");
  189.         Required = FALSE;
  190.         logBuf.lbflags.AIDE = OnOff("Aide setting");
  191.  
  192.         /* NULLs */
  193.         if (GetAString(line, sizeof line, fd) != NULL) {
  194.  
  195.                 if (!isdigit(line[0]))
  196.                         crashout("The NULLs value doesn't seem to be a numeral.");
  197.                 logBuf.lbnulls = atoi(line);
  198.         }
  199.         else logBuf.lbnulls = 0;
  200.  
  201.         /* LD credits */
  202.         if (GetAString(line, sizeof line, fd) != NULL) {
  203.  
  204.                 if (!isdigit(line[0]))
  205.                         crashout("The NULLs value doesn't seem to be a numeral.");
  206.                 logBuf.credit = atoi(line);
  207.         }
  208.         else logBuf.credit = 0;
  209.  
  210.         logBuf.lbflags.NET_PRIVS = OnOff("Net privs setting");
  211.         logBuf.lbflags.DOOR_PRIVS = OnOff("Door privs setting");
  212.         logBuf.lbflags.DL_PRIVS = OnOff("DL privs setting");
  213.         logBuf.lbflags.PERMANENT = OnOff("Permanent account setting");
  214.         logBuf.lbflags.HALF_DUP = OnOff("Half-duplex setting");
  215.         logBuf.lbflags.TWIT = OnOff("Twit setting");
  216.  
  217.         /* Delay stuff */
  218.         if (GetAString(line, sizeof line, fd) != NULL) {
  219.  
  220.                 if (!isdigit(line[0]))
  221.                         crashout("The delay value doesn't seem to be a numeral.");
  222.                 logBuf.lbdelay = atoi(line);
  223.         }
  224.         else logBuf.lbdelay = 0;
  225.         logBuf.lbflags.ALT_RE = OnOff(".ECZ setting");
  226. }
  227.  
  228. /*
  229.  * OnOff()
  230.  *
  231.  * This handles parsing an On/Off parameter.  If interpretation fails it
  232.  * crashes out.
  233.  */
  234. char OnOff(char *where)
  235. {
  236.         char line[200];
  237.  
  238.         if (GetAString(line, sizeof line, fd) == NULL) {
  239.                 sprintf(line, "File ended abruptly while reading %s.", where);
  240.                 crashout(line);
  241.         }
  242.  
  243.         if (stricmp(line, "on") == SAMESTRING)
  244.                 return TRUE;
  245.         if (stricmp(line, "off") == SAMESTRING)
  246.                 return FALSE;
  247.  
  248.         if (Required) {
  249.                 sprintf(line, "Malformed value for %s.", where);
  250.                 crashout(line);
  251.         }
  252.  
  253.         return FALSE;
  254. }
  255.  
  256. /*
  257.  * AddAccount()
  258.  *
  259.  * This function adds a new account to the system.
  260.  */
  261. void AddAccount()
  262. {
  263.         int good, ourSlot, i, g;
  264.         MSG_NUMBER  low;
  265.         char tmp[30];
  266.         SYS_FILE    checkHeld;
  267.         char   *LCHeld = "log%d.hld";
  268.  
  269.         for (good = cfg.MAXLOGTAB-1; good >= 0; good--)
  270.                 if (!logTab[good].ltpermanent) break;
  271.  
  272.         if (good < 0) good = cfg.MAXLOGTAB - 1; /* too bad */
  273.  
  274.         ourSlot = logTab[good].ltlogSlot;
  275.  
  276.         slideLTab(0, good);
  277.         logTab[0].ltlogSlot = ourSlot;
  278.  
  279.         thisLog = ourSlot;
  280.  
  281.         /* copy info into record:       */
  282.         logBuf.lbflags.L_INUSE    = TRUE;
  283.  
  284.         low = cfg.newest-50;
  285.         if (cfg.oldest - low < 0x8000)   low = cfg.oldest;
  286.         for (i=1;  i<MAXVISIT;  i++)   logBuf.lbvisit[i]= low;
  287.         logBuf.lbvisit[ 0]= cfg.newest;
  288.         logBuf.lbvisit[ (MAXVISIT-1)]= cfg.oldest;
  289.  
  290.         /* initialize rest of record:   */
  291.         for (i = 0;  i < MAXROOMS;  i++) {
  292.             if (roomTab[i].rtflags.PUBLIC == 1) {
  293.                 g = (roomTab[i].rtgen);
  294.                 logBuf.lbgen[i] = (g << GENSHIFT) + (MAXVISIT-1);
  295.             } else {
  296.                 /* set to one less */
  297.                 g = (roomTab[i].rtgen + (MAXGEN-1)) % MAXGEN;
  298.                 logBuf.lbgen[i] = (g << GENSHIFT) + (MAXVISIT-1);
  299.             }
  300.         }
  301.         memset(logBuf.lbMail, 0, MAIL_BULK);
  302.  
  303.         /* fill in logTab entries       */
  304.         i = 0;
  305.         logTab[i].ltpwhash      = hash(logBuf.lbpw)  ;
  306.         logTab[i].ltnmhash      = hash(logBuf.lbname);
  307.         logTab[i].ltlogSlot     = ourSlot       ;
  308.         logTab[i].ltnewest      = logBuf.lbvisit[0];
  309.         logTab[i].ltpermanent   = logBuf.lbflags.PERMANENT;
  310.  
  311. #ifdef MAIL_ALSO
  312.         /* automatic mail for the new user. */
  313.         makeSysName(checkHeld, (logBuf.lbflags.EXPERT) ? "expmail.blb" :
  314.                                         "novmail.blb", &cfg.homeArea);
  315.         if (access(checkHeld, 0) == 0) {
  316.             i = thisRoom;
  317.             getRoom(MAILROOM);
  318.             ZeroMsgBuffer(&msgBuf);
  319.             ingestFile(checkHeld, &msgBuf);
  320.             strCpy(msgBuf.mbauth, (strLen(cfg.SysopName)) ?
  321.                                     cfg.SysopName : "Citadel");
  322.             strCpy(msgBuf.mbto, logBuf.lbname);
  323.             putMessage(lBuf);
  324.             getRoom(i);
  325.         }
  326. #endif
  327.  
  328.         storeLog();
  329.  
  330.         if (cfg.BoolFlags.HoldOnLost) {
  331.                 sprintf(tmp, LCHeld, ourSlot);
  332.                 makeSysName(checkHeld, tmp, &cfg.holdArea);
  333.                 unlink(checkHeld);
  334.         }
  335. }
  336.  
  337. /*
  338.  * slideLTab()
  339.  *
  340.  * This function slides bottom N lots in logTab down.  For sorting.
  341.  */
  342. void slideLTab(int slot, int last)
  343. {
  344.     int i;
  345.  
  346.     /* open slot up: (movmem isn't guaranteed on overlaps) */
  347.     for (i = last - 1;  i >= slot;  i--)  {
  348.         memcpy(&logTab[i + 1], logTab + i,(long)cfg.sizeLTentry);
  349.     }
  350. }
  351.  
  352. /*
  353.  * storeLog()
  354.  *
  355.  * This function stores the current log record.
  356.  */
  357. void storeLog()
  358. {
  359.     logTab[0].ltnewest    = cfg.newest;
  360.     logBuf.lbvisit[0]     = cfg.newest;
  361.     putLog(&logBuf, thisLog);
  362. }
  363.  
  364. /*
  365.  * crashout()
  366.  *
  367.  * Fatal error?  Out we go!
  368.  */
  369. void crashout(char *s)
  370. {
  371.         printf("Ooops: %s\n", s);
  372.         exit(1);
  373. }
  374.